package com.HuangJiaQing.www.service.impl;

import com.HuangJiaQing.www.dao.GoodsMapper;
import com.HuangJiaQing.www.dao.WareHouseMapper;
import com.HuangJiaQing.www.model.Goods;
import com.HuangJiaQing.www.model.Marketers;
import com.HuangJiaQing.www.model.WareHouse;
import com.HuangJiaQing.www.model.WareHouseExample;
import com.HuangJiaQing.www.service.WareHouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author HONOR
 * @date 2022/8/5 17:54
 */
@Service
public class WareHouseServiceImpl implements WareHouseService {
    @Autowired
    private WareHouseMapper wareHouseMapper;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private HttpSession session;
    @Autowired
    private ThreadPoolTaskExecutor taskExecutor;

    public List<WareHouse> selectWareHouseByMarketersId(Integer marketersId) {
        WareHouseExample wareHouseExample = new WareHouseExample();
        wareHouseExample.createCriteria().andMarketerIdEqualTo(marketersId);
        List<WareHouse> wareHouses = wareHouseMapper.selectByExample(wareHouseExample);
        return wareHouses;
    }

    public void insertWareHouse(WareHouse wareHouse) {
        wareHouseMapper.insertSelective(wareHouse);
    }

    public Boolean insertGoodsById(Integer id) {
        WareHouse wareHouse = wareHouseMapper.selectByPrimaryKey(id);
        if (wareHouse.getIsForbidWarehouse() > 0) {
            return false;
        } else {
            Goods goods = new Goods();
            goods.setMarketersId(wareHouse.getMarketerId());
            goods.setAvatar(wareHouse.getAvatar());
            goods.setGoodsName(wareHouse.getGoodsName());
            goods.setGoodsPrice(wareHouse.getGoodsPrice());
            goods.setNumber(wareHouse.getSum());
            Marketers marketers = (Marketers) session.getAttribute("marketers");
            goods.setMarketersName(marketers.getMarketersName());
            int i = goodsMapper.insertSelective(goods);
            if (i>0){
                wareHouseMapper.deleteByPrimaryKey(wareHouse.getWarehouseId());
                return true;
            }else {
                return false;
            }
        }
    }

    public void deleteWareHouseById(Integer id) {
        wareHouseMapper.deleteByPrimaryKey(id);
    }

    public List<WareHouse> searchWareHouse(String search) {
        WareHouseExample wareHouseExample = new WareHouseExample();
        wareHouseExample.createCriteria().andGoodsNameLike("%" + search + "%");
        List<WareHouse> wareHouses = wareHouseMapper.selectByExample(wareHouseExample);
        return wareHouses;
    }

    public List<WareHouse> selectWareHouseForbid() {
        Marketers marketers = (Marketers) session.getAttribute("marketers");
        WareHouseExample wareHouseExample = new WareHouseExample();
        wareHouseExample.createCriteria().andIsForbidWarehouseEqualTo(1).andMarketerIdEqualTo(marketers.getMarketersId());
        List<WareHouse> wareHouses = wareHouseMapper.selectByExample(wareHouseExample);
        return wareHouses;
    }

    public WareHouse selectWareHouseById(Integer id) {
        WareHouse wareHouse = wareHouseMapper.selectByPrimaryKey(id);
        return wareHouse;
    }

    public void updateWareHouse(WareHouse wareHouse) {
        wareHouseMapper.updateByPrimaryKeySelective(wareHouse);
    }

    public List<WareHouse> selectWareHouseNeedAudit() {
        WareHouseExample wareHouseExample = new WareHouseExample();
        wareHouseExample.createCriteria().andIsForbidWarehouseEqualTo(2);
        List<WareHouse> wareHouses = wareHouseMapper.selectByExampleWithMarketers(wareHouseExample);
        return wareHouses;
    }

    public void scheduledTasks(final Integer id, String time) {
        final WareHouse wareHouse = wareHouseMapper.selectByPrimaryKeyWithMarketers(id);
        if (wareHouse.getIsForbidWarehouse()>0){
            return;
        }
        time = time.replace("T", " ");
        final long l = System.currentTimeMillis();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date parse = null;
        try {
            parse = df.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        final long t = parse.getTime();
        if (t > l) {
            taskExecutor.execute(new Runnable() {
                public void run() {
                    try {
                        Thread.sleep(t-l);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    Goods goods = new Goods();
                    goods.setMarketersId(wareHouse.getMarketerId());
                    goods.setAvatar(wareHouse.getAvatar());
                    goods.setGoodsName(wareHouse.getGoodsName());
                    goods.setGoodsPrice(wareHouse.getGoodsPrice());
                    goods.setMarketersName(wareHouse.getMarketers().getMarketersName());
                    goods.setNumber(wareHouse.getSum());
                    goodsMapper.insertSelective(goods);
                    wareHouseMapper.deleteByPrimaryKey(wareHouse.getWarehouseId());
                }
            });

        }
    }

}
